package cc.ryanc.halo.service.impl;

import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.vo.PostPage;
import cc.ryanc.halo.model.vo.Postpipeline;
import cc.ryanc.halo.repository.PostRepository;
import cc.ryanc.halo.service.ReptileMessageService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.OOSpider;

import java.util.List;

/**
 * @author ckj
 */
@Service
public class ReptileMessageServiceImpl implements ReptileMessageService {
    Logger logger = LoggerFactory.getLogger(ReptileMessageServiceImpl.class);


    @Autowired
    private PostRepository postRepository;


    @Override
    public Integer startReptile(String keyName) {

        logger.info("==startReptile==" + keyName);
        Postpipeline pageModelPipeline = new Postpipeline();
        OOSpider ooSpider = OOSpider.create(Site.me(), pageModelPipeline, PostPage.class);
        ooSpider.addUrl("https://www.oschina.net/news/").run();
        List<Post> posts = pageModelPipeline.getPosts();
        if (CollectionUtils.isNotEmpty(posts)) {
            posts.forEach(v -> {
                Post v1 = v;
                postRepository.save(v1);
            });
        }
        return 1;
    }

    //控制台输出
//    public static void main(String[] args) {
//        OOSpider ooSpider = OOSpider.create(Site.me(), new ConsolePageModelPipeline(), PostPage.class);
//        ooSpider.addUrl("https://www.oschina.net/news").run();
//    }
}
